package com.bizfx.utils
{
	import mx.collections.IList;
	import mx.controls.Tree;

	public class TreeUtils
	{
		public static function openItem(item:Object, openItems:Array, openLevel:int, level:int=0):void {
			if (openLevel <= 0 || openLevel > level) {
				if (item != null && item.hasOwnProperty("children")) {
					if(item.children.length > 0) {
						openItems.push(item);
						
						if (item.children is IList) {
							for each(var child:Object in item.children) {
								openItem(child, openItems, openLevel, level+1);
							}
						} else if (item.children is Array) {
							for (var i:int; i < item.children.length; i++) {
								openItem(item.children[i], openItems, openLevel, level+1);
							}
						}
					}
				}
			}
		}
		
		public static function openItems(tree:Object, openLevel:int):void {
			if (tree.dataProvider == null) {
				return;
			}
			var openItems:Array = new Array();
			if (tree.dataProvider is IList) {
				for each(var item:Object in tree.dataProvider) {
					if (item) {
						openItem(item, openItems, openLevel);
					}
				}
			}
			tree.openItems = openItems;
		}
	}
}